package Maze;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Growing_grass2 {
    static int[] dx = {0, 1, 0, -1};
    static int[] dy = {1, 0, -1, 0};

    public static void main(String[] args) throws IOException {
        BufferedReader scanner = new BufferedReader(new InputStreamReader(System.in));
        String[] n_m = scanner.readLine().split(" ");
        int n = Integer.parseInt(n_m[0]);
        int m = Integer.parseInt(n_m[1]);
        node[][] map = new node[n][m];
        for (int i = 0; i < n; i++) {
            String line = scanner.readLine();
            char[] charArray = line.toCharArray();
            for (int j = 0; j < charArray.length; j++) {
                map[i][j] = new node(i, j, String.valueOf(charArray[j]));
            }
        }
        String[] strings = scanner.readLine().split(" ");
        int k = Integer.parseInt(strings[0]);
        bfs(map, k);
        print(map);
    }

    public static void bfs(node[][] map, int k) {
        ArrayList<node> arrayList = new ArrayList<>();
        while (k != 0) {
            for (int i = 0; i < map.length; i++) {
                for (int j = 0; j < map[i].length; j++) {
                    node node = map[i][j];
                    if (node.getValue().equals("g")) {
                        arrayList.add(node);
                    }
                }
            }

            while (!arrayList.isEmpty()) {
                node temp = arrayList.get(0);
                int x = temp.getX();
                int y = temp.getY();
                for (int b = 0; b <= 3; b++) {
                    int tx = x + dx[b];
                    int ty = y + dy[b];
                    if (
                            tx >= 0 && tx < map.length &&
                                    ty >= 0 && ty < map[0].length &&
                                    !map[tx][ty].getValue().equals("g")) {
                        map[tx][ty].setValue("g");
                    }
                }
                arrayList.remove(0);
            }
            k--;
        }
    }

    static class node {
        int x;
        int y;
        String value;

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public node(int x, int y, String value) {
            this.x = x;
            this.y = y;
            this.value = value;
        }
    }

    private static void print(node[][] map) {
        for (node[] line : map) {
            for (node node : line) {
                System.out.print(node.getValue());
            }
            System.out.println();
        }
    }
}


